package cn.xxlucky.xssdk01.demo.controller;


import cn.xxlucky.xssdk01.constant.XfConstance;
import cn.xxlucky.xssdk01.demo.service.IXfAIService;
import cn.xxlucky.xssdk01.pojo.ApiRequest;
import cn.xxlucky.xssdk01.pojo.ApiResponse;
import cn.xxlucky.xssdk01.pojo.shard.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * @author Kiven xxluckey.cn
 */
@RestController
@RequestMapping("/test/xf")
public class TestXfController {


    @Autowired
    private IXfAIService iXfAIService;

    @Value("${xf.model}")
    private String model;

    /**
     * 流式请求
     * 在后端的流式接口中，确保 @GetMapping 注解的 produces 设置为 MediaType.TEXT_EVENT_STREAM_VALUE。
     * @param
     * @return
     */
    @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public ResponseBodyEmitter streamLargeModelResponse(String input, HttpServletResponse response) {
        // 设置正确的 Content-Type
        response.setHeader("Content-Type", "text/event-stream;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Connection", "keep-alive");
        ResponseBodyEmitter emitter = new ResponseBodyEmitter();

        //异步调用服务
        new Thread(() -> {
            try {
                ApiRequest apiRequest = new ApiRequest();
                // 可以在yml配置所用的模型
                apiRequest.setModel(model);
                // 约等于100字
                //apiRequest.setMaxTokens(150);
                List<Message> user = new ArrayList<>();
                // 可以根据业务的不同准备不同的ROLE和提示词
                //Message message1 = new Message(XfConstance.ROLE, XfConstance.WA_CALL_WORD);
                // 设置用户输入的内容
                Message message = new Message(XfConstance.USER, input);
                user.add(message);
                //user.add(message1);
                apiRequest.setMessages(user);

                iXfAIService.sendRequest(emitter, apiRequest);
            } catch (Exception e) {
                emitter.completeWithError(e);
            }
        }).start();

        return emitter;
    }

    /**
     * 普通AI问答
     * @param input
     * @return
     */
    @GetMapping("/ai")
    public ApiResponse callApi(String input) {
        // 创建请求对象
        ApiRequest apiRequest = new ApiRequest();
        apiRequest.setModel(model);
        // 约等于100字
        //apiRequest.setMaxTokens(150);
        List<Message> user = new ArrayList<>();
        //Message message1 = new Message(XfConstance.ROLE, XfConstance.WA_CALL_WORD);
        Message message = new Message(XfConstance.USER, input);
        user.add(message);
        //user.add(message1);
        apiRequest.setMessages(user);

        // 调用API
        return iXfAIService.sendRequest(apiRequest);
    }
}
